<?php
$assetsUrl = Yii::app()->getModule('qa')->assetsUrl;
$cs = Yii::app()->clientScript;
$cs->registerCssFile("$assetsUrl/css/typography.css");
?>

<div class="form full">

	<?php
	$form = $this->beginWidget('CActiveForm',
		array(
		'id'=>'qa-question-form',
		'enableAjaxValidation'=>true,
		));
	?>

	<?php echo $form->errorSummary($model); ?>

	<table class="collapse">
		<tr>
			<td class="top">
				<div class="column">
					<div class="row">
						<?php
						$default = Yii::app()->request->hostInfo;
						$default .= $assetsUrl;
						$default .= '/images/gravatar-150.png';
						$userClass = Yii::app()->getModule('qa')->userClass;
						$userModel = call_user_func(array($userClass, 'model'));
						$user = $userModel->findByPk(Yii::app()->user->id);
						$firstname = CHtml::encode($user->profile->firstname);
						$config = array(
							'email'=>$user->email,
							'hashed'=>false,
							'default'=>$default,
							'size'=>150,
							'rating'=>'PG',
							'htmlOptions'=>array(
								'alt'=>"$firstname Avatar",
								'title'=>$firstname,
								'class'=>'round stroke',
							),
						);
						$this->widget('qa.widgets.GravatarWidget', $config);
						?>
						<div class="center">
							<b><?php echo $firstname; ?></b>
						</div>
					</div>
				</div>
			</td>
			<td class="full">
				<div class="row">
					<?php
					echo $form->textField(
						$model, 'title', array('size'=>60, 'maxlength'=>255)
					);
					echo $form->error($model, 'title');
					?>
				</div>

				<div class="row">
					<?php
					echo $form->textArea(
						$model, 'value', array('rows'=>15, 'cols'=>50)
					);
					echo $form->error($model, 'value');
					?>
				</div>

				<div class="right">
					<?php
					$url = 'http://daringfireball.net/projects/markdown/syntax';
					$htmlOptions = array(
						'target'=>'_blank',
					);
					echo CHtml::link('MarkDown Help', $url, $htmlOptions);
					?>
				</div>

				<div class="row buttons">
					<?php
					$isNew = $model->isNewRecord;
					echo CHtml::submitButton($isNew ? 'Submit' : 'Save');
					
					$htmlOptions = array('id'=>'preview');
					echo CHtml::button('Preview', $htmlOptions);
					?>
				</div>

				<div class="clear"></div>

			</td>
		</tr>
	</table>


	<?php $this->endWidget(); ?>


</div><!-- form -->

<?php
$properties = array(
	'id'=>'question-preview',
	'options'=>array(
		'title'=>'Preview',
		'width'=>'800',
		'height'=>'400',
		'autoOpen'=>false,
		'resizable'=>true,
		'modal'=>true,
		'hide'=>'blind',
		'show'=>'blind',
	),
);
$this->beginWidget('zii.widgets.jui.CJuiDialog', $properties);
$view = 'application.modules.qa.views.question._preview';
echo $this->renderPartial($view);
$this->endWidget('zii.widgets.jui.CJuiDialog');

$data = array(
	':titleId'=>CHtml::activeId($model, 'title'),
	':valueId'=>CHtml::activeId($model, 'value'),
);
$cs->registerScriptContent(
	'_form', 'application.modules.qa.views.question._form',
	CClientScript::POS_READY, $data
);
$assetsUrl = Yii::app()->getModule('qa')->assetsUrl;
$cs->registerScriptFile("$assetsUrl/js/Markdown.Converter.js");
?>
